package com.leo.neo4j.config;


import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;

/**
 * neo4j，支持事务，但是@Transactional注解不支持多个事务管理器，默认使用transactionManager，需要实现@Transactional管理mysql事务
 *
 * @Author Created by YangMeng on 2021/3/3 18:58
 */
@Configuration
@EnableTransactionManagement
public class Neo4jConfig {
    /**
     * 定义neo4j事务
     *
     * @param sessionFactory
     * @return
     */
    @Bean("neo4jTransactionManager")
    public Neo4jTransactionManager neo4jTransactionManager(SessionFactory sessionFactory) {
        return new Neo4jTransactionManager(sessionFactory);
    }

    /**
     * 定义mysql 事务
     *
     * @param emf
     * @return
     */
    @Bean("transactionManager")
    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory emf) {
        return new JpaTransactionManager(emf);
    }
}